<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="handleQuery"
      @clear="clear"
    />
    <div class="doumee_table_header">
      <el-button
        v-if="meta.indexOf('MemberInOutLeave') > -1"
        :loading="leaveLoading"
        type="primary"
        @click="MemberInOutLeave"
        >{{ $t("message.lichan") }}</el-button
      >
      <el-button
        v-if="meta.indexOf('MemberInOutRecordExport') > -1"
        :loading="exportLoading"
        @click="MemberInOutRecordExport"
        >{{ $t("message.daochu") }}</el-button
      >
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        fixed="left"
        align="center"
      />
      <el-table-column
        align="center"
        :label="$t('message.rcsj')"
        prop="inTime"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.memberInfo')"
        prop="member"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.rcpz')"
        prop="inFlag"
        min-width="100"
      >
        <template v-slot="scope">
          <span v-if="scope.row.inFlag == '42'">{{ $t("message.mp") }}</span>
          <span v-else-if="scope.row.inFlag == '32'">{{
            $t("message.cdyd")
          }}</span>
          <span v-else-if="scope.row.inFlag == '41'">{{
            $t("message.tc")
          }}</span>
          <span v-else>{{ $t("message.mp") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.chanpin')"
        prop="productName"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.ddbh')"
        prop="orderId"
        min-width="210"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.rcfs')"
        min-width="120"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.inType == '1'">{{ $t("message.ewm") }}</span>
          <span v-if="scope.row.inType == '2'">{{ $t("message.rl") }}</span>
          <span v-if="scope.row.inType == '3'">{{ $t("message.ic") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.zcsj')"
        prop="durationOfPresence"
        min-width="160"
      />
      <el-table-column
        align="center"
        :label="$t('message.lcsj')"
        prop="outTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        :label="$t('message.hxry')"
        prop="editor"
        min-width="120"
      />
      <el-table-column
        align="center"
        :label="$t('message.hysb')"
        prop="verificationDevInfo"
        min-width="140"
      />
      <el-table-column
        align="center"
        :label="$t('message.hyqd2')"
        prop="channel"
        min-width="120"
      />
      <el-table-column
        :label="$t('message.caozuo')"
        min-width="100"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="
                meta.indexOf('MemberMealsCancelUse') > -1 &&
                scope.row.inFlag == '41' &&
                scope.row.flagInfo
              "
              v-preventReClick
              type="text"
              @click="toCx(scope.row)"
              >{{ $t("message.chexiao") }}</el-button
            >
            <span v-else>-</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <Pagination
        :total="totalCount"
        :page="pagination.page"
        @current-change="handlePageChange"
        @SizeChange="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import {
  inOutRecord,
  InOutLeave,
  InOutRevoke,
  inOutRecordExport,
  getProList,
} from "@/api";
import dayjs from "dayjs";
import { queryFormConfig } from "./config";
import Pagination from "@/components/paginations";
import QueryForm from "@/components/QueryForm";
export default {
  name: "MemberInOutRecord",
  components: {
    QueryForm,
    Pagination,
  },
  data() {
    return {
      meta: [],
      list: [],
      selectList: [],
      totalCount: 0,
      listLoading: false,
      exportLoading: false,
      leaveLoading: false,
      pagination: {
        page: 1,
        rows: 10,
      },
      queryFormConfig,
      filters: {
        daterange: [],
      },
    };
  },
  created() {
    this.meta = this.$route.meta.buttons || [];
    this.filters.daterange.push(dayjs(new Date()).format("YYYY-MM-DD"));
    this.filters.daterange.push(dayjs(new Date()).format("YYYY-MM-DD"));
    this.getList();
    this.getProductList();
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout();
    });
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout();
    });
  },
  methods: {
    getProductList() {
      getProList({
        pagination: {
          page: 1,
          rows: 1000,
          firstQueryTime: this.filters.firstQueryTime,
        },
        param: {
          // status: "1",
        },
      })
        .then((res) => {
          if (res.errorCode === "000000") {
            const arrb = res.recordList;
            arrb.forEach((e) => {
              e.label = e.name;
              e.value = e.id;
            });
            this.queryFormConfig.formItems[3].options = arrb;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    toCx(e) {
      const that = this;
      this.$confirm(this.$t("message.cxddtc"), this.$t("message.ts"), {
        confirmButtonText: this.$t("message.qd"),
        cancelButtonText: this.$t("message.qx"),
        type: "warning",
      })
        .then(() => {
          InOutRevoke({
            param: {
              mealsMemberId: e.flagInfo,
              businessId: e.id,
            },
          })
            .then((res) => {
              if (res.errorCode === "000000") {
                that.$message.success(this.$t("message.cxcg"));
                that.getList();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        });
    },
    handleSizeChange(value) {
      this.pagination.rows = value;
      this.getList();
    },
    handlePageChange(value) {
      this.pagination.page = value;
      this.getList();
    },
    handleSelectionChange(val) {
      this.selectList = val;
    },
    handleQuery() {
      this.pagination.page = 1;
      this.getList();
    },
    getList() {
      const { filters, pagination } = this;
      if (filters.daterange.length > 0) {
        filters.starTime = filters.daterange[0];
        filters.endTime = filters.daterange[1];
      }
      this.listLoading = true;
      inOutRecord({
        pagination,
        param: {
          ...filters,
          venueId: sessionStorage.getItem("venueId"),
        },
      })
        .then((res) => {
          this.listLoading = false;
          if (res.errorCode === "000000") {
            this.list = res.recordList;
            this.totalCount = res.totalCount;
          }
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    MemberInOutRecordExport() {
      const { filters } = this;
      this.exportLoading = true;
      inOutRecordExport({
        param: { ...filters },
      }).then(
        (res) => {
          if (res.errorCode !== "000000") return;
          this.$message.success(this.$t("message.dccg"));
          if (window.Android) {
            Android.downloadFile(res.record.showUrl);
            this.exportLoading = false;
            return;
          }

          const a = document.createElement("a"); // 创建一个a标签元素
          a.style.display = "none"; // 设置元素不可见
          a.href = res.record.showUrl; // 设置下载地址
          document.body.appendChild(a); // 加入
          a.click(); // 触发点击,下载
          document.body.removeChild(a);
          // window.open(res.record.showUrl, '_blank')
          this.exportLoading = false;
        },
        () => {
          this.exportLoading = false;
        }
      );
    },
    MemberInOutLeave() {
      if (this.selectList.length === 0) {
        return this.$message.warning(this.$t("message.qxxzlcjlk"));
      }
      const ids = this.selectList.map((i) => i.id);
      this.leaveLoading = true;
      InOutLeave({
        param: {
          ids,
        },
      }).then(
        (res) => {
          this.leaveLoading = false;
          if (res.errorCode !== "000000") return;
          this.$message.success(this.$t("message.pllccg"));
          this.getList();
        },
        () => {
          this.leaveLoading = false;
        }
      );
    },
    clear() {
      this.filters = { daterange: [] };
      this.getList();
    },
  },
};
</script>
